using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace ReexaminationProject
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        // Predefined fields
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        // Game states
        enum GameStates
        {
            TitleScreen,
            Background
        }
        GameStates currentGameState;

        // Texture fields
        Texture2D titleScreen;

        // Lists of textures
        List<Texture2D> selectCharacter;
        List<Texture2D> backgrounds;
        int currentBackground = 1;

        // Texture tables
        Texture2D[][] characterTextures;
        
        // Random numbers generator
        Random rand;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            rand = new Random();
            currentGameState = GameStates.TitleScreen;
            backgrounds = new List<Texture2D>();
            selectCharacter = new List<Texture2D>();
            base.Initialize();
        }

        // Using a folder name input this method will initializa a table with rows as long as the number of sprites in each subfolder
        void initializeTables(string directory)
        {
            int i = 0;
            Texture2D[][] array1 = new Texture2D[Directory.GetDirectories(directory).Length][];
            foreach (string dir in Directory.GetDirectories(directory))
            {
                string[] s = Directory.GetFiles(dir.ToString());
                array1[i] = new Texture2D[Directory.GetFiles(dir.ToString()).Length];
            }
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            titleScreen = Content.Load<Texture2D>(@"Textures/TitleScreen/title");
            loadList("Backgrounds", backgrounds);
            loadList("SelectCharacter", selectCharacter);
            // TODO: use this.Content to load your game content here
        }

        void loadList(string directory, List<Texture2D> listToBeFilled)
        {
            foreach (string file in Directory.GetFiles(@"C:\Users\BIGSTONE\Documents\Visual Studio 2010\Projects\ReexaminationProject\ReexaminationProject\ReexaminationProjectContent\Textures\" + directory))
            {
                listToBeFilled.Add(Content.Load<Texture2D>(@"Textures\" + directory + @"\" + Path.GetFileNameWithoutExtension(file)));
            }
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
            
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            if (currentGameState == GameStates.TitleScreen && Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                currentBackground = rand.Next(1, 18);
                currentGameState = GameStates.Background;
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            if (currentGameState == GameStates.TitleScreen)
            {
                // Drawing the titleScreen image in the beginning of the game
                spriteBatch.Draw(titleScreen, new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height), Color.White); 
            }
            if (currentGameState == GameStates.Background)
            {
                // Drawing the random background for the round
                spriteBatch.Draw(backgrounds[currentBackground], new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height), Color.White);
            }
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
